package com.future.mayi.fragment;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

public class RecycleAllOrderFragment extends BaseFragment {

    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    Unbinder unbinder;
    private int style;

    private List<RecycleItemOrderFragment> fragmentList ;
    private static String[] TITLE;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_recycle_all_order;
    }

    @Override
    protected void initView() {

        if (style == 2)
            TITLE = new String[]{"全部", "待报价", "已报价", "已寄件", "已完成"};
        else
            TITLE = new String[]{"全部", "待报价", "已报价", "已寄件", "寄卖中", "已完成"};

        fragmentList = new ArrayList<>(6);
        for (int i = 0; i < TITLE.length; i++) {
            RecycleItemOrderFragment goodsAllOrderFragment = new RecycleItemOrderFragment();
            goodsAllOrderFragment.setStyle(style,i);
            fragmentList.add(goodsAllOrderFragment);
        }
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });

        initIndicator();

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    public void setStyle(int style) {
        this.style = style;
    }

    private void initIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return TITLE.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(TITLE[index]);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.main_text_color));

                simplePagerTitleView.setPadding(CommonUtils.dip2px(getActivity(), 22), 0,
                        CommonUtils.dip2px(getActivity(), 22), 0);
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.main_color));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.main_color));
                indicator.setRoundRadius(CommonUtils.dip2px(getActivity(), 1));
                indicator.setLineWidth(CommonUtils.dip2px(getActivity(), 20));
                indicator.setLineHeight(CommonUtils.dip2px(getActivity(), 2));
                indicator.setMode(2);
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }


    public void refresh() {
        if (fragmentList != null && fragmentList.size() > 0 )
            fragmentList.get(viewPager.getCurrentItem()).refresh();
    }
}
